package prj.carmap.mobile.process;

import org.json.JSONException;
import org.json.JSONObject;

import prj.carmap.mobile.CarmapApplication;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

/**
 * 快捷键处理
 *
 */
public class HotKeyProcess extends AsyncHttpResponseHandler {

	private static AsyncHttpClient client = new AsyncHttpClient();

	public void getHotKeyConfig() {
		CarmapApplication.config.hotColor = "#0096D5";
		CarmapApplication.config.hotterColor = "#D9772C";
		client.get(CarmapApplication.SERVER_URL + "/rest/config", this);
	}

	/**
	 * Fired when a request returns successfully, override to handle in your own code
	 * @param content the body of the HTTP response from the server
	 * @throws JSONException
	 */
	public void onSuccess(String content) {
		try
		{
			JSONObject json = new JSONObject(content);
			if (json.has("hotColor"))
			{
				CarmapApplication.config.hotColor = json.getString("hotColor");
			}
			else
			{
				CarmapApplication.config.hotColor = "#0096D5";
			}

			if (json.has("hotterColor"))
			{
				CarmapApplication.config.hotterColor = json.getString("hotterColor");
			}
			else
			{
				CarmapApplication.config.hotterColor = "#D9772C";
			}
		}
		catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Fired when a request fails to complete, override to handle in your own code
	 * @param error the underlying cause of the failure
	 * @param content the response body, if any
	 */
	public void onFailure(Throwable error, String content) {

	}

}
